<?php
namespace Admin\Controller;
use Think\Controller;
/**
* FieldController
*/
class FieldController extends CommonController
{
	//初始化
	public function _initialize() {
		//字段类型存放目录
		$this->fields = MODULE_PATH . 'Fields/';
		$modelid = I('get.modelid', 0, 'intval');
		$this->assign('modelid', $modelid);
	}
	//显示字段列表
	public function index() {
		$modelid = I('get.modelid', 0, 'intval');
		if (empty($modelid)) {
			alertMes('参数错误！');
		}
		$model = M("Model")->where(array("modelid" => $modelid))->find();
		if (empty($model)) {
			alertMes('该模型不存在！');
		}
		//根据模型读取字段列表
		$data = D('Field')->getModelField($modelid);
		$this->assign("data", $data);
		$this->display();
	}
	//增加字段
	public function add()
	{
		//模型ID
		$modelid = I('request.modelid', 0, 'intval');
		if (empty($modelid)) {
			alertMes('模型ID不能为空！');
		}
		if (IS_POST) {
			$post = $_POST;
			if (empty($post)) {
			    alertMes('数据不能为空！');
			}
			if (D('Field')->addField($post)) {
			    alertMes("添加成功！", "/admin/Field/index/modelid/$modelid");
			} else {
			    $error = D('Field')->getError();
			    alertMes($error ? $error : '添加失败！');
			}
		} else {
			//可使用字段类型
			$all_field = D('Field')->getFieldTypeList();
			$this->assign("all_field", $all_field);
			$this->display();
		}
	}
	//编辑字段
	public function edit()
	{
		//模型ID
        $modelid = I('modelid', 0, 'intval');
        //字段ID
        $fieldid = I('fieldid', 0, 'intval');
        if (empty($modelid)) {
            alertMes('模型ID不能为空！');
        }
        if (empty($fieldid)) {
            alertMes('字段ID不能为空！');
        }
		if (IS_POST) {
			$post = $_POST;
			if (empty($post)) {
			    alertMes('数据不能为空！');
			}
			if (D('Field')->editField($post, $fieldid)) {
			    alertMes("修改成功！", "/admin/Field/index/modelid/$modelid");
			} else {
			    $error = D('Field')->getError();
			    alertMes($error ? $error : '修改失败！');
			}
		} else {
			//模型信息
            $modedata = M("Model")->where(array("modelid" => $modelid))->find();
            if (empty($modedata)) {
                alertMes('该模型不存在！');
            }
            //字段信息
            $fieldData = D('Field')->where(array("fieldid" => $fieldid, "modelid" => $modelid))->find();
            if (empty($fieldData)) {
                alertMes('该字段信息不存在！');
            }
            $setting = unserialize($fieldData['setting']);
            //打开缓冲区
            ob_start();
            include $this->fields . $fieldData['formtype'] . '.inc.php';
            $form_data = ob_get_contents();
            //关闭缓冲
            ob_end_clean();
			//可使用字段类型
			$all_field = D('Field')->getFieldTypeList();
			$this->assign("setting", $setting);
			$this->assign("form_data", $form_data);
			$this->assign("all_field", $all_field);
			$this->assign("fieldid", $fieldid);
			$this->assign("data", $fieldData);
			$this->display();
		}
	}
	//删除字段
    public function del() {
    	$modelid = I('get.modelid', 0, 'intval');
        //字段ID
        $fieldid = I('get.fieldid', 0, 'intval');
        if (empty($fieldid)) {
            alertMes('字段ID不能为空！');
        }
        if (D('Field')->deleteField($fieldid)) {
            alertMes("字段删除成功！","/admin/Field/index/modelid/$modelid");
        } else {
            $error = D('Field')->getError();
            alertMes($error ? $error : "删除字段失败！");
        }
    }
    //模型预览
    public function priview() {
        //模型ID
        $modelid = I('get.modelid');
        if (empty($modelid)) {
            $this->error("请指定模型！");
        }
        $fielddata = D('Field')->getModelField($modelid);
        foreach ($fielddata as $key => $value) {
        	$data[$key] = D('Field')->priviewform($value);
        }
        $this->assign('dataform',$data);
        $this->display();
    }
    //加载字段文件
	public function getFieldPath($type)
	{
		$file = $this->fields . $type . '.inc.php';
		if (file_exists($file)) {
			ob_start();
			include $file;
			$setting = ob_get_contents();
			ob_end_clean();
			echo $setting;
		}
	}
	//字段排序
    public function listorder($modelid) {
    	//模型ID
    	$modelid = I('request.modelid', 0, 'intval');
    	if (empty($modelid)) {
    		alertMes('模型ID不能为空！');
    	}
        if (IS_POST) {
            foreach ($_POST['listorders'] as $id => $listorder) {
                D('Field')->where(array('fieldid' => $id))->save(array('listorder' => $listorder));
            }
            alertMes("排序成功","/admin/field/index/modelid/$modelid");
        } else {
            alertMes("排序失败");
        }
    }
    //显示隐藏字段
    public function disabled()
    {
    	$modelid = I('get.modelid', 0, 'intval');
        //字段ID
        $fieldid = I('get.fieldid', 0, 'intval');
        if (empty($fieldid)) {
            alertMes('字段ID不能为空！');
        }
        $disabled = D('Field')->where(array('modelid' => $modelid, 'fieldid' => $fieldid))->getField('disabled');
        if ($disabled) {
        	$arr['disabled'] = 0;
        } else {
        	$arr['disabled'] = 1;
        }
        $res = D('Field')->where(array('modelid' => $modelid, 'fieldid' => $fieldid))->save($arr);
        if ($res) {
            alertMes("字段状态更改成功！","/admin/Field/index/modelid/$modelid");
        } else {
            $error = D('Field')->getError();
            alertMes($error ? $error : "删除状态更改失败！");
        }
    }
}